#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"


//1.创建一个空的单向链表(有头单向链表)
link_node_t *CreateEpLinkList(){
    link_node_t* p=(link_node_t*)malloc(sizeof(link_node_t));
    if(p == NULL){
        printf("分配失败！\n");
        return NULL;
    }
    p->data=0;
    p->next=NULL;
    return p;
}

//2.向单向链表的指定位置插入数据
//p保存链表的头指针 post 插入的位置 data插入的数据
int InsertIntoPostLinkList(link_node_t *p,int post, datatype data){
    if(p == NULL){
        printf("头指针错误！\n");
        return -1;
    }
    link_node_t* newcode=(link_node_t*)malloc(sizeof(link_node_t));
    if(newcode == NULL){
        printf("插入节点内存分配失败！\n");
        return -1;
    }
    newcode->data=data;
    newcode->next=NULL;
    link_node_t* agoposition=p;
    int postpsition=-1;
    while(agoposition->next != NULL  && postpsition <= post-1){
        agoposition=agoposition->next;
        postpsition++;
    }
    if(postpsition != post-1){
        printf("插入位置不合法！\n");
        free(newcode);
        newcode=NULL;
        return -1;
    }
    newcode->next=agoposition->next;
    agoposition->next=newcode;
    // printf("插入成功！\n");
    return 0;
}

//3.遍历单向链表
void ShowLinkList(link_node_t *p){
    if(p == NULL){
        printf("头指针错误！\n");
        return;
    }
    if(p->next == NULL){
        printf("链表为空\n");
        return;
    }
    int i=0;
    while(p->next != NULL){
        p=p->next;
        i++;
        printf("第%d个节点的data:%d \n",i,p->data);
        
    }
}

//4.求单向链表长度的函数
int LengthLinkList(link_node_t *p){
    if(p == NULL){
        printf("头指针错误！\n");
        return -1;
    }
    if(p->next == NULL){
        printf("链表为空\n");
        return -1;
    }
    int len=0;
    while(p->next != NULL){
        p=p->next;
        len++;
    }
    return len; 
}

//5.删除单向链表中指定位置的数据 post 代表的是删除的位置
int DeletePostLinkList(link_node_t *p, int post){
    if(p == NULL){
        printf("头指针错误！\n");
        return -1;
    }
    if(p->next == NULL){
        printf("链表为空\n");
        return -1;
    }
    link_node_t* agoposition=p;
    int postpsition=0;
    while(agoposition->next != NULL && postpsition <post-1){
        agoposition=agoposition->next;
        postpsition++;
    }
    link_node_t* nowposition=agoposition->next;
    link_node_t* afposition=nowposition->next;
    if(postpsition != post-1 ){
        printf("删除位置不在链表范围！\n");
        return 0;
    }
    agoposition->next=afposition;
    free(nowposition);
    nowposition=NULL;
    return -1;
}

//6.判断单向链表是否为空 1代表空 0代表非空
int IsEpLinkList(link_node_t *p){
    if(p == NULL){
        printf("头指针错误！\n");
        return -1;
    }
    if(p->next == NULL){
        printf("链表为空！\n");
        return 1;
    }
    else{
        printf("链表非空！\n");
        return 0;
    }

}

//7.修改指定位置的数据 post 被修改的位置 data修改成的数据
int ChangePostLinkList(link_node_t *p, int post, datatype data){
    if(p == NULL){
        printf("头指针错误！\n");
        return -1;
    }
    if(p->next == NULL){
        printf("链表为空\n");
        return -1;
    }
    link_node_t* nowposition=p;
    int postpsition=0;
    while(nowposition->next != NULL && postpsition <post){
        nowposition=nowposition->next;
        postpsition++;
    }
    if(postpsition != post ){
        printf("修改位置不在链表范围！\n");
        return 0;
    }
    nowposition->data=data;
    return 1;
}

//8.查找指定数据出现的位置 data被查找的数据 //search 查找
int SearchDataLinkList(link_node_t *p, datatype data){
    if(p == NULL){
        printf("头指针错误！\n");
        return -1;
    }
    if(p->next == NULL){
        printf("链表为空\n");
        return -1;
    }
    link_node_t* nowposition=p;
    int postpsition=0;
    while(nowposition->next != NULL){
        nowposition=nowposition->next;
        postpsition++;
        if(nowposition->data == data){
            return postpsition;
        }
    }
    nowposition=NULL;
    printf("未找到！\n");
    return 0;
}

//9.删除单向链表中出现的指定数据,data代表将单向链表中出现的所有data数据删除
int DeleteDataLinkList(link_node_t *p, datatype data){
    if(p == NULL){
        printf("头指针错误！\n");
        return -1;
    }
    if(p->next == NULL){
        printf("链表为空\n");
        return -1;
    }
    int panduan=0;
    link_node_t* agoposition=p;
    link_node_t* nowposition=NULL;
    link_node_t* afposition=NULL;
    while(agoposition->next != NULL){
        nowposition=agoposition->next;
        if(nowposition->data == data){
            agoposition->next=nowposition->next;
            free(nowposition);
            panduan=0;
        }
        else{
            agoposition=agoposition->next;
        }
        
    }
    if(panduan == 1){
        printf("未找到！\n");
        return 0;
    }
    
}

//10.转置链表
void ReverseLinkList(link_node_t *p){
    if(p == NULL){
        printf("头指针错误！\n");
        return;
    }
    if(p->next == NULL){
        printf("链表为空\n");
        return;
    }
    
    link_node_t* agoposition=NULL;
    link_node_t* nowposition=p->next;
    while(nowposition != NULL){
        link_node_t* next=nowposition->next;
        nowposition->next=agoposition;
        agoposition=nowposition;
        nowposition=next; 
    }
    p->next=agoposition;
}

//11.清空单向链表
void ClearLinkList(link_node_t *p){
    if(p == NULL){
        printf("头指针错误！\n");
        return;
    }
    if(p->next == NULL){
        printf("链表为空\n");
        return;
    }
    link_node_t* nowposition=p->next;
    while(nowposition != NULL){
        link_node_t* next=nowposition->next;
        free(nowposition);
        nowposition=next;
    }
    p->next=NULL;
}
